local MAJOR_VERSION = "LibInternalAddonCooldown-1.0"
local MINOR_VERSION = 90000 + tonumber(("$Revision: 1 $"):match("%d+")) or 0

--[[

  USAGE
  -----
  
  ADDING TO YOUR ADDON
  local liac = LibStub("LibInternalAddonCooldown-1.0")
  
  CHECKING A COOLDOWN
    if(liac:GetCooldown("UpysRaidTools", "TardCheck", "RandomMageName") > 0)then
      print("Tard Check is on Cooldown :( /sadface")
    end
  
  ADDING A COOLDOWN
    -- Random Code that found out RandomMageName is a tard and /yell at him
    liac:AddCooldown("UpysRaidTools", "TardCheck", "RandomMageName", 30)
  
  COMBINING TO TWO FUNCTIONS
  By adding this to a commonly used event such as COMBAT_LOG_EVENT_UNFILTERED this will /yell every 30 seconds
  
    if(liac:GetCooldown("UpysRaidTools", "TardCheck", "RandomMageName") == 0)then
      -- /yell YOU'RE A TARD MAGE!!!
      liac:AddCooldown("UpysRaidTools", "TardCheck", "RandomMageName", 30)
    end



  TABLE SUPPORTS 2 TYPES OF COOLDOWNS
  -----------------------------------
  
  GENERAL COOLDOWNS - SINGLE FOR A NAME
  SUB COOLDOWNS - MULTIPLE (BY TOON?) FOR A NAME
  
  SUB COOLDOWNS WILL BE USED WHEN A REFERENCE IS PASSED

  cooldowns = {
    ["Addon"] = {
      ["Name"] = 20110312191211,
      ["Name2"] = {
        ["toon"] = 20110312191211,
        ["toon2"] = 20110312191211,
      }
    }
  }
  
]]


local lib, oldMinor = LibStub:NewLibrary(MAJOR_VERSION, MINOR_VERSION)
if not lib then return end

local cooldowns = {}
local curtime
local secondsleft

local function GetCurrentTime()
  return time{year=date("%Y"), month=date("%m"), day=date("%d"), hour=date("%H"), min=date("%M"), sec=date("%S")}
end

local function CleanupCooldowns(addon, name, ref)

  -- If either addon or name is empty then we don't want to know about it
  if not (addon or name) then return end
  
  -- Get our Cooldown Time
  curtime = GetCurrentTime()

  -- If the name table doesnt exist then exit (nothing to clean up anyways)
  if not(cooldowns[addon][name])then return end
  
  -- check our name
  t = cooldowns[addon][name]
  if(type(t) == "table")then
    for k, v in pairs(t) do
      -- If the v is a table we are using a ref (toon name?)
      if(curtime > v) then cooldowns[addon][name][k] = nil end
    end
  else
    if(curtime > cooldowns[addon][name]) then cooldowns[addon][name] = nil end
  end
  
end

local function GetCooldownLeft(addon, name, ref)
    -- Get our Cooldown Time
  curtime = GetCurrentTime()
  secondsleft = nil
  
  if(ref)then
    secondsleft = cooldowns[addon][name][ref] - curtime
  else
    secondsleft = cooldowns[addon][name] - curtime
  end
  
  return secondsleft
  
end

function lib:GetCooldown(addon, name, ref)

  -- IF NO ADDON OR NAME
  if not (addon or name) then print("LibInternalCooldown-1.0 Warning: Invalid Use for lib:InternalCooldowns(addon, name, ref, cooldown). no addon or name provided. Returned 0") return 0 end
  
  -- TABLE SETUP
  if not (cooldowns[addon])then cooldowns[addon] = {} end
  
  -- CHECKS AND CLEANUPS
  CleanupCooldowns(addon, name, ref)
  
  -- IF NO NAME RECORD EXIT
  if not(cooldowns[addon][name])then return 0 end
  
  -- IF REF IS PROVIDED CHECK IF WE HAVE A COOLDOWN
  if(ref)then
    if not(cooldowns[addon][name][ref])then return 0 end
  end
  
  -- We found some data so return that we're on cooldown
  return GetCooldownLeft(addon, name, ref)
  
end

function lib:AddCooldown(addon, name, ref, cooldown)
 
   -- IF NO ADDON OR NAME
  if not (addon or name) then print("LibInternalCooldown-1.0 Warning: Invalid Use for lib:InternalCooldowns(addon, name, ref, cooldown). no addon or name provided. Returned 0") return false end
  
  -- Get our Cooldown Time
  curtime = GetCurrentTime()
  
  -- TABLE SETUP
  if not (cooldowns[addon])then cooldowns[addon] = {} end
  
  -- CHECKS AND CLEANUPS
  CleanupCooldowns(addon, name, ref)
  
  -- ADD OUR COOLDOWN (DIFF FOR GENERAL OR SUB)
  if(ref)then
    if not (cooldowns[addon][name])then cooldowns[addon][name] = {} end
    cooldowns[addon][name][ref] = curtime + cooldown
  else
    cooldowns[addon][name] = curtime + cooldown
  end
  
end

function lib:OutputCooldowns(addon, name, ref)

  for a, an in pairs(cooldowns) do
    print('["' .. a .. '"] = {')

      for n, r in pairs(an) do
        print(' ["' .. n .. '"] = {')

          if(type(r) == "table")then
            print('  ["' .. n .. '"] = {')
            for k, v in pairs(r) do
              print('   ["' .. k .. '"] = ' .. v .. ',')
            end
            print('  },')
          else
            print('  ["' .. n .. '"] = ' .. r .. ',')
          end
        
        print(' },')
      end

    print('},')
  end

end